package com.rotai.messengerclient;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.concurrent.atomic.AtomicBoolean;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private static final int MSG_SUM = 0x110;

    private Button mBtnAdd;
    private LinearLayout mLyContainer;

    //显示连接状态
    private TextView mTvState;

    private Messenger mService;
    private AtomicBoolean isConn = new AtomicBoolean(false);

    @SuppressLint("HandlerLeak")
    private Messenger mMessenger = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msgFromService) {
            switch (msgFromService.what) {
                case MSG_SUM:
                    TextView tv = mLyContainer.findViewById(msgFromService.arg1);
                    tv.setText(tv.getText() + "=>" + msgFromService.arg2);
                    break;
            }
            super.handleMessage(msgFromService);
        }
    });

    private ServiceConnection mConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = new Messenger(service);
            isConn.set(true);
            mTvState.setText("connected!");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            isConn.set(false);
            mTvState.setText("disconnected");
        }
    };

    private int mA;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        intent.setAction("com.rotai.MessengerService");
        intent.setPackage("com.rotai.messengerclient");
        bindService(intent, mConn, BIND_AUTO_CREATE);

        mTvState = findViewById(R.id.id_tv_callback);
        mBtnAdd = findViewById(R.id.id_btn_add);
        mLyContainer = findViewById(R.id.id_ll_container);

        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = mA++;
                int b = (int) (Math.random() * 100);

                TextView tv = new TextView(MainActivity.this);
                tv.setText(a + "+" + b + "=caculating...");
                tv.setId(a);
                tv.setTextColor(Color.parseColor("#337af6"));
                mLyContainer.addView(tv);

                Message msgFromClient = Message.obtain(null, MSG_SUM, a, b);
                msgFromClient.replyTo=mMessenger;
               if(isConn.get()){
                   try {
                       mService.send(msgFromClient);
                   } catch (RemoteException e) {
                       e.printStackTrace();
                   }
               }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConn);
    }
}
